package com.anuo.app.modules.coach.service;


import com.anuo.app.common.basedata.AuditState;
import com.anuo.app.common.persistence.PageData;
import com.anuo.app.common.thirdparty.PushUtil;
import com.anuo.app.modules.coach.dao.StudentDao;
import com.anuo.app.modules.coach.dao.SystemManageDao;
import com.anuo.app.modules.coach.entity.Coach;
import com.anuo.app.modules.coach.entity.CoachComment;
import com.anuo.app.modules.coach.entity.CoachFilter;
import com.anuo.app.modules.coach.entity.CoachOrganization;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;


@Service
@Transactional(readOnly = true)
public class SystemManageService {
    @Autowired
    SystemManageDao systemManageDao;
    @Autowired
    CoachService coachService;

    @Transactional(readOnly = false)
    public void saveCoachAuditRefuse(Coach coach) {
        coach.setState(AuditState.REFUSE);
        coach.setAudit_datetime(new Date());
        systemManageDao.coachAuditRefuse(coach);
    }

    @Transactional(readOnly = false)
    public void saveCoachAuditPass(Coach coach) {
        //修改审核记录
        coach.setState(AuditState.PASS);
        coach.setAudit_datetime(new Date());
        systemManageDao.coachAuditPass(coach);
        //将审核记录转为正式记录
        if (!coachService.isExistCoachInfo(coach.getId())) {
            //不存在就添加一条正式记录
            systemManageDao.insertCoachFromCoachApply(coach);
        } else {
            //存在就修改原有正式记录
            systemManageDao.updateCoachByCoachApply(coach);
        }
        //删除申请信息
        systemManageDao.deleteCoachApply(coach);
        //极光推送审核通过
        PushUtil.sendPush(coach.getId(),"您的教练申请已审核通过,恭喜您成为教练");
    }

    @Transactional(readOnly = true)
    public boolean isExistOrganizationName(CoachOrganization coachOrganization) {
        return systemManageDao.isExistOrganizationName(coachOrganization);
    }

    @Transactional(readOnly = false)
    public void saveCoachOrganization(CoachOrganization coachOrganization) {
        if (StringUtils.isEmpty(coachOrganization.getId())){
            coachOrganization.preInsert();
            systemManageDao.insertCoachOrganization(coachOrganization);
        }else if (StringUtils.isNotEmpty(coachOrganization.getId())){
            systemManageDao.updateCoachOrganization(coachOrganization);
        }

    }

}